package com.jelly.common.util.upload;

import java.io.*;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.jelly.common.exception.UploadImgException;
import com.jelly.web.modules.basics.websiteDetails.domain.WebUpload;
import org.springframework.web.multipart.MultipartFile;
/**
 * @创建人 ql
 * @创建时间 2018-10-18 14:26
 * @项目名称 jelly
 * @功能描述: OSS上传工具
 **/
public class OSSClientUtil {

    /**
    * 获取阿里云OSS客户端对象
    * @return
    */
    public static OSSClient getOSSClient(WebUpload webUpload){
        return new OSSClient(webUpload.getEndpoint(),webUpload.getAccessKeyId(), webUpload.getAccessKeySecret());
    }

    /**
    * 创建存储空间
    * @param ossClient  OSS连接
    * @param bucketName  存储空间
    * @return
    */
      public static String createBucketName(OSSClient ossClient,String bucketName){
           final String bucketNames=bucketName;
           if(!ossClient.doesBucketExist(bucketName)){
               //创建存储空间
               Bucket bucket=ossClient.createBucket(bucketName);
               return bucket.getName();
           }
           return bucketNames;
      }
    /**
    *
    * @param ossClient
    * @param bucketName
    */
    public static void deleteBucket(OSSClient ossClient,String bucketName){
        ossClient.deleteBucket(bucketName);
    }
 
    /**
    * 创建模拟文件夹
    * @param ossClient
    * @param bucketName
    * @param folder
    * @return
    */
    public static String createFolder(OSSClient ossClient,String bucketName,String folder){
      //文件夹名
      final String keySuffixWithSlash=folder;

          //判断文件夹是否存在，不存在则创建
          if(!ossClient.doesObjectExist(bucketName,keySuffixWithSlash)){
               //创建文件夹
               ossClient.putObject(bucketName,keySuffixWithSlash,new ByteArrayInputStream(new byte[0]));
               //得到文件夹名
               OSSObject object = ossClient.getObject(bucketName,keySuffixWithSlash);
               String fileDir=object.getKey();
                return fileDir;
          }
         return keySuffixWithSlash;
      }
 
          /**
    * 根据key删除OSS服务器上的文件
    * @param ossClient
    * @param bucketName
    * @param folder
    * @param key
    */
    public static void deleteFile(OSSClient ossClient,String bucketName,String folder, String key){
       ossClient.deleteObject(bucketName,folder+key);
    }
 
    /**
    * 上传图片至OSS
    * @param ossClient
    * @param file
    * @param bucketName
    * @param folder
    * @return
    */
    public static String uploadObject2OSS(OSSClient ossClient,MultipartFile file,String bucketName,String folder,String type ){
        String resultStr = null;
        try {
            String fileName = file.getOriginalFilename();
            Long fileSize = file.getSize();
            ObjectMetadata metadata= new ObjectMetadata();
            metadata.setContentLength(fileSize);
            metadata.setCacheControl("no-cache");
            metadata.setHeader("Pragma", "no-cache");
            metadata.setContentEncoding("utf-8");
            metadata.setContentType(type);
            metadata.setContentDisposition("filename/filesize="+fileName+"/"+fileSize+"Byte.");
            String path = folder+getcontentType(type)+fileName;
            //上传文件
            ossClient.putObject(bucketName,path, file.getInputStream(),metadata);
            resultStr = path;
        } catch (IOException e) {
            throw new UploadImgException(e.getMessage());
        }

        return resultStr;
    }

    /**
     * 文件上传时文件的contentType
     * @param type 类型 (user:用户头像)
     * @return
     */
    public static String getcontentType(String type) {
        if ("user".equals(type)) {
            return "image/user-info/";
        }
        return "common/";
    }


    //调用该方法进行上传
    public static String  uploadImg(MultipartFile headPic, WebUpload webUpload,String type) {
        OSSClient ossClient = getOSSClient(webUpload);
        String md5key = uploadObject2OSS(ossClient, headPic, webUpload.getBacketName(), webUpload.getPath(),type);
        return md5key;
    }

}
